package ma.mindware.mpm.domain.account;

import java.util.HashMap;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import ma.mindware.met.core.account.User;
import ma.mindware.met.core.technicalservice.ServiceRunner;
import ma.mindware.met.core.technicalservice.TechnicalServiceException;
import ma.mindware.met.core.technicalservice.UserDBService;
import ma.mindware.met.core.util.Crypter;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class AccountController extends ActionSupport {
	
	private ServiceRunner service;
	private Object serviceParameters[];
	private HttpSession session =ServletActionContext.getRequest().getSession();
	private User user;
	private String wrongCredentials;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getWrongCredentials() {
		return wrongCredentials;
	}

	public void setWrongCredentials(String wrongCredentials) {
		this.wrongCredentials = wrongCredentials;
	}

	
	private Properties getConfig(){
		Properties config=((Properties)session.getServletContext().getAttribute("config"));
		return config;
	}
	
	private ServiceRunner getDBService(){
		service= ServiceRunner.getInstance();
		serviceParameters=new Object[1]; 
		serviceParameters[0]=getConfig().getProperty("dbInfoFilePath");
		return service;
	}
	
	public String logIn(){
		ServletContext context     = session.getServletContext();
		HashMap<Long,HttpSession> connectedUser=(HashMap<Long,HttpSession>) context.getAttribute("users");
		
		if(connectedUser==null){
			connectedUser=new HashMap<Long, HttpSession>();
		}
		Crypter crypter=new Crypter();
		user.setPassword(crypter.hashPassword(user.getPassword()));
		System.out.println(user.getPassword());
		try {
			user=(User) getDBService().run(UserDBService.class, serviceParameters, "getUserSession", user);
			// recuperer le role de l'utilisateur 
			
			//
			if(user!=null){//success login
				session.setAttribute("user", user);
				connectedUser.put(user.getAccountID(), session);
				context.setAttribute("users", connectedUser);
				this.getUserRole(user);
				return SUCCESS;
			}else{//login failed
				wrongCredentials=getText("general.wrongCredentials");
				return "failed";
			}
		} catch (TechnicalServiceException e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	
	public String logOut(){
		session.invalidate();
		return SUCCESS;
	}
	
	/**
	 * Recuperer le role de l'utilisateur
	 */

	private void getUserRole(User user){
		try {
			String role=(String) getDBService().run(AccountDBService.class, serviceParameters, "getUserRole", user);
			if(!role.equals("")){
				session.setAttribute("userRole", role);
			}
		} catch (TechnicalServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	

}
